package com.ling.core.view.builder;

import com.bstek.dorado.view.widget.grid.Column;
import com.bstek.dorado.view.widget.grid.DataGrid;
import com.bstek.dorado.view.widget.grid.GridSupport;
import com.ling.core.view.ViewComponent;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @author bo.wang
 * @since 2013-2-18
 */
@Component
public class GridBuilder extends AbstractControlBuilder {
    public void build(Object control, ViewComponent parentViewComponent) {
        GridSupport grid = (GridSupport) control;
        String id = grid.getId();
        ViewComponent component = new ViewComponent();
        component.setId(id);
        component.setIcon(">dorado/res/" + DataGrid.class.getName().replaceAll("\\.", "/") + ".png");
        component.setName(DataGrid.class.getSimpleName());
        parentViewComponent.addChildren(component);
        if (StringUtils.isEmpty(id)) {
            component.setEnabled(false);
        }
        for (Column col : grid.getColumns()) {
            for (IControlBuilder builder : builders) {
                if (builder.support(col)) {
                    builder.build(col, component);
                    break;
                }
            }
        }
    }

    public boolean support(Object control) {
        return control instanceof GridSupport;
    }
}
